package cn.djb.day7;

import cn.djb.day6.TreeNode;
import javafx.scene.transform.Rotate;

import java.util.Stack;

/**
 * 请完成一个函数，输入一个二叉树，该函数输出它的镜像
 */
class Solution_2 {

    /**
     * 递归 深度遍历
     *
     * @param root
     * @return
     */
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(temp);

        return root;

    }

    /**
     * 建立辅助栈
     * @param root
     * @return
     */
    public TreeNode mirrorTree2(TreeNode root) {
        if (root == null) return null;
        Stack<TreeNode> nodes = new Stack<>();
        nodes.add(root);

        while (!nodes.isEmpty()) {
            TreeNode node = nodes.pop();
            if (node.left != null) nodes.push(node.left);
            if (node.right != null) nodes.push(node.right);

            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;

        }
        return root;

    }


}